﻿
using System.Threading.Channels;
namespace UtilsMQ
{
    /// <summary>
    /// 进程内 的消息队列https://www.cnblogs.com/cdaniu/p/15902976.html、http://www.manongjc.com/detail/13-epgytdtbruhkazm.html
    /// </summary>
    public class ServiceThreadChannelForSerial
    {
        //100条消息，有限，达到100后，会保存，等少于100 再自动写入， CreateUnbounded为无限，直至服务器内存耗尽
        public static Channel<string> channels = Channel.CreateBounded<string>(100);
        /// <summary>
        /// 写入队列
        /// </summary>
        /// <param name="value"></param>
        public static void writeChannel(string value)
        {
            channels.Writer.WriteAsync(value);
        }

        /// <summary>
        /// 读取队列
        /// </summary>
        /// <returns></returns>
        public static string readChannel()
        {
            string reault = "";
            if (channels.Reader.TryRead(out reault))
            {
                return reault;
            }
            return reault;
        }
        public static int readChannelsCount()
        {
            return channels.Reader.Count;
        }
    }

}
